package com.heatup.apt.management.service.channer.impl;

import java.util.Date;
import java.util.List;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import com.heatup.apt.common.dao.CourseNoticeInfoMapper;
import com.heatup.apt.common.dao.store.StoreInfoMapper;
import com.heatup.apt.management.service.channer.CourseNoticeInfoService;
import com.heatup.apt.model.channer.CourseNoticeInfo;
import com.heatup.apt.model.store.StoreInfo;

import com.baomidou.framework.service.impl.SuperServiceImpl;
import com.baomidou.mybatisplus.mapper.EntityWrapper;
import com.baomidou.mybatisplus.plugins.Page;

/**
 *
 * CourseNoticeInfo 表数据服务层接口实现类
 *
 */
@Service
public class CourseNoticeInfoServiceImpl extends SuperServiceImpl<CourseNoticeInfoMapper, CourseNoticeInfo>
		implements CourseNoticeInfoService {

	@Autowired
	private StoreInfoMapper storeInfoMapper;
	@Autowired
	private CourseNoticeInfoMapper courseNoticeInfoMapper;

	@Override
	public boolean saveCourseNoticeInfo(CourseNoticeInfo courseNoticeInfo) {
		if (courseNoticeInfo.getId() == null) {
			int isExistCount = courseNoticeInfoMapper.isExistsUnCompleteCourseNoitce().intValue();
			// 如果存在未完成的课程通知，则不允许增加新的 课程通知。
			if (isExistCount > 0) {
				return false;
			} else {
				String title = courseNoticeInfo.getTitle();
				if (title == null || "".equals(title)) {
					title = "下周的檬课表更新啦，快来和檬教练们一起锻炼吧！";
				}
				String storeids = courseNoticeInfo.getStoreids();
				if (null == storeids || "".equals(storeids)) {
					storeids = "";
					EntityWrapper<StoreInfo> entityWrapper = new EntityWrapper<StoreInfo>();
					entityWrapper.where("status={0}", "2");
					//example.or().andStatusEqualTo("2");
					List<StoreInfo> storeInfos = 
							//storeInfoMapper.selectByExample(example);
					storeInfoMapper.selectList(entityWrapper);
					if (storeInfos != null) {
						for (StoreInfo storeInfo : storeInfos) {
							storeids += storeInfo.getStoreName() + ",";
						}
						storeids=storeids.substring(0, storeids.length() - 1);
					} else {
						return false;
					}
				}
				courseNoticeInfo.setTitle(title);
				courseNoticeInfo.setStoreids(storeids);
				int count = courseNoticeInfoMapper.insertSelective(courseNoticeInfo);
				return count > 0;
			}
		} else {
			CourseNoticeInfo old = courseNoticeInfoMapper.selectById(courseNoticeInfo.getId());
			if (old != null && old.getSendFlag().intValue() == 1) {
				int count = courseNoticeInfoMapper.updateSelectiveById(courseNoticeInfo);
				return count > 0;
			} else {
				return false;
			}
		}
		
	}
	
	public Page<CourseNoticeInfo> queryCourseNoticeInfoPages(Page<CourseNoticeInfo> courseNoticeInfoPages){
			courseNoticeInfoPages.setRecords(courseNoticeInfoMapper.queryCourseNoticeInfoByPages(courseNoticeInfoPages));
		return courseNoticeInfoPages;
	}

	@Override
	public CourseNoticeInfo queryCourseNoticeInfoById(int id) {
		return courseNoticeInfoMapper.selectById(id);
	}

	@Override
	public boolean updateCourseNoticeStatusInfo(int id,int userId) {
		CourseNoticeInfo courseNoticeInfo = courseNoticeInfoMapper.selectById(id);
		if(courseNoticeInfo !=null && courseNoticeInfo.getSendFlag().intValue() == 1){
			courseNoticeInfo.setSendFlag(3);
			courseNoticeInfo.setUpdateBy(Integer.valueOf(userId));
			courseNoticeInfo.setUpdateTime(new Date());
			return courseNoticeInfoMapper.updateSelectiveById(courseNoticeInfo)>0;
		}else{
			return false;
		}
	}

}